<template>
	<view class="content home">
		<!-- 头部轮播 -->
		<view class="home-swiper">
			<swiper autoplay="true" circular="true" class="swiperContent">
				<swiper-item v-for="(img,key) in imgUrls" :key="key">
					<image :src="img" class="swiperImg" />
				</swiper-item>
			</swiper>
		</view>

		<!-- 通知公告 -->
		<view class="home-notice">
			<uni-notice-bar show-icon scrollable show-get-more :moreText="$t('common.buttons.more')"
				:text="noticeTitle" :speed="50" @click="getNoticeDetail" @getmore="getNoticeMore">
			</uni-notice-bar>
		</view>

		<!-- 工作统计 -->
		<view class="home-apply">
			<view class="apply-title">
				<image src="../../../static/images/icon/more-ico.png"></image>
				{{$t('app.homepage.workStatistics')}}
			</view>
			<template v-if="appType == 'single' && showCount">
				<work-stat-inspect :taskCount="taskCount" />
			</template>
			<template v-if="appType == 'multiple' && showCount">
				<view class="flexRow" v-if="workList.length > 0">
					<menu-entry v-for="(item, index) in workList" :key="index" :menu="item" @toMenu="toMenu"></menu-entry>
				</view>
				<view class="no-data" v-else>{{$t('app.homepage.noApplication')}}</view>
			</template>
		</view>

		<!-- 常用应用 -->
		<view class="home-apply">
			<view class="apply-title">
				<image src="../../../static/images/icon/more-ico.png"></image>
				{{$t('app.homepage.commonApplications')}}
			</view>
			<view class="flexRow" v-if="favoritesList.length > 0">
				<menu-entry v-for="(item, index) in favoritesList" :key="index" :menu="item" @toMenu="toMenu"></menu-entry>
			</view>
			<view class="no-data" v-else>{{$t('app.homepage.noApplication')}}</view>
		</view>

		<!-- 智慧运维 -->
		<view v-if="showExtra">
			<intelligent-operation></intelligent-operation>
		</view>
		
		<!-- 更新 -->
		<uni-popup ref="upgrade">
			<upgrade-popup @close="closeUpgrade" @downloadSuccess="downloadSuccess"></upgrade-popup>
		</uni-popup>
	</view>
</template>

<script>
	import workStatInspect from '@/components/business/expand/work-statistics-inspect.vue'
	import intelligentOperation from '@/components/business/expand/intelligent-operation.vue'
	import upgradePopup from "@/components/base/upgrade-popup.vue"
	import { getNews, getFavoritesResource, getBasicInfo, postDownSucessInfo } from '@/api/platform/common.js'
	import { utilities, business } from '@/utils/common.js'
	import { getPendingTasks } from '@/api/base/flow.js'

	export default {
		components: {
			workStatInspect,
			intelligentOperation,
			upgradePopup
		},
		data() {
			return {
				imgUrls: [],
				noticeId: '',
				noticeTitle: '',
				workList: [],
				favoritesList: [],
				taskCount: '',
				showCount: true,
				latestVersion: {},
				backButtonPress: 0
			}
		},
		onLoad() {
			this.$nextTick(() => {
				this.init()
			})
		},
		onShow() {
			// 获取消息数量
			business.refreshMsgCount()

			// 获取代办数量
			this.getTaskCount()

			const _this = this
			uni.$once('goWebview', function(data) {
				_this.goWebview(data.title, data.url)
			})
		},
		computed: {
			appType() {
				return utilities.getConfig('PROJECT').APP_TYPE
			},
			showExtra() {
				return utilities.getConfig('PROJECT').SHOW_EXTRA
			},
			systemName() {
				return utilities.getConfig('PROJECT').SYSTEM_NAME
			},
			systemBanner() {
				return utilities.getConfig('PROJECT').BANNER
			},
			needUpgrade() {
				return utilities.getConfig('PROJECT').NEED_UPGRADE
			}
		},
		methods: {
			init() {
				// 页面标题
				uni.setNavigationBarTitle({
					title: this.systemName
				})

				// 页面轮播
				this.imgUrls = this.systemBanner

				//获取公告
				getNews().then(res => {
					if (res.data.length > 0) {
						this.noticeId = res.data[0].id
						this.noticeTitle = res.data[0].title
					}
				})

				// 获取工作统计
				if (this.appType == 'multiple') {
					let menuList = uni.getStorageSync("menuList").filter(item => {
						return item.alias == 'gztj'
					})
					this.workList = menuList.length > 0 ? menuList[0].children : []
				}

				//获取常用应用
				let menuList = uni.getStorageSync("menuList").filter(item => {
					return item.alias == 'yy'
				})
				this.favoritesList = menuList.length > 0 ? menuList[0].children : []
				// getFavoritesResource().then(res=>{
				// 	res.data.dataResult.forEach(item => {
				// 		let menu = menuList.children.find(r => r.id == item.id)
				// 		this.favoritesList.push(menu)
				// 	})
				// })
				
				// 获取基本信息
				getBasicInfo(uni.getStorageSync('userId')).then(res => {
					uni.setStorageSync("stationList", res.data.stationList ? res.data.stationList : [])
					uni.setStorageSync("teamList", res.data.teamList ? res.data.teamList : [])
					uni.setStorageSync("depList", res.data.depList ? res.data.depList : [])
					uni.setStorageSync("companyId", res.data.company ? res.data.company.companyId : '')
					uni.setStorageSync("companyName", res.data.company ? res.data.company.companyName : '')
					uni.setStorageSync("pagePathList", res.data.pagePathList ? res.data.pagePathList : [])
				})
				
				// 是否需要更新
				if(this.needUpgrade) {
					let currentVersion = uni.getStorageSync("currentVersion")
					this.latestVersion = uni.getStorageSync("latestVersion")
					if(utilities.compareVersion(currentVersion, this.latestVersion.appVersion) == -1) {
						this.$refs.upgrade.open()
					}
				}
				
				// 项目类型 wind：风电，fire：火电
				uni.setStorageSync("projectType", utilities.getConfig('PROJECT').PROJECT_TYPE)
			},
			closeUpgrade() {
				this.$refs.upgrade.close()
			},
			downloadSuccess() {
				this.latestVersion.updateUser = uni.getStorageSync("userId")
				postDownSucessInfo(this.latestVersion)
			},
			getNoticeDetail() {
				if (this.noticeId) {
					uni.navigateTo({
						url: '/pages/platform/notice/detail?noticeId=' + this.noticeId
					})
				}
			},
			getNoticeMore() {
				uni.navigateTo({
					url: '/pages/platform/notice/index'
				})
			},
			getTaskCount() {
				let pager = {
					start: 1,
					limit: 5000
				}
				let params = []
				let badgeNumber = 0
				this.showCount = false
				getPendingTasks(pager, params, false).then(res => {
					if (res.data.pageResult) {
						badgeNumber = res.data.pageResult.totalCount
						this.taskCount = res.data.pageResult.totalCount + ""
						if (this.taskCount > 100) {
							this.taskCount = "99+"
						}
					} else {
						badgeNumber = 0
						this.taskCount = ""
					}
					this.showCount = true

					// #ifdef APP-PLUS
					// 设置应用角标
					plus.runtime.setBadgeNumber(badgeNumber)
					// #endif
				})
			},
			toMenu(menu) {
				if (menu.alias == 'wzgl') {
					uni.navigateTo({
						url: '/pages/business/material/index/index'
					})
				}
				if (menu.defaultUrl) {
					console.log(menu.defaultUrl)
					uni.navigateTo({
						url: menu.defaultUrl
					})
				}
			},
			goWebview(title, url) {
				uni.navigateTo({
					url: '/pages/base/webview?title=' + title + '&url=' + encodeURIComponent(url)
				})
			}
		},
		onBackPress() {
			this.backButtonPress++;
			if (this.backButtonPress > 1) {
				// 退出清除角标
				plus.runtime.setBadgeNumber(0);
				// 退出清除token
				uni.removeStorageSync('_app_token');
				plus.runtime.quit();
			} else {
				plus.nativeUI.toast(this.$t('app.message.pressAgainExit'));
			}
			return true;
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f1f1f1;
	}
	
	.home {
		display: flex;
		flex-direction: column;

		.home-swiper {
			.swiperContent {
				height: 150px;
			}

			.swiperImg {
				width: 750rpx;
				height: 150px;
			}
		}

		.home-notice {
			/deep/ .uni-noticebar {
				margin-bottom: 0;
			}
		}

		.home-apply {
			margin-top: 10px;
			background: #fff;

			.apply-title {
				color: #2e6d5d;
				padding: 8px;
				font-size: $uni-font-size-lg;
				font-weight: bold;
				border-bottom: 1px solid #eee;
				display: flex;
				align-items: center;

				image {
					width: 25px;
					height: 25px;
				}
			}

			.flexRow {
				display: flex;
				flex-wrap: wrap;
			}
			
			.no-data {
				margin: 20px 0;
				text-align: center;
				font-size: 16px;
				color: #353535;
			}
		}
	}
</style>
